//
//  AppDelegate+NTESService.m
//  Bees
//
//  Created by Xiaohan on 2018/12/13.
//  Copyright © 2018 xiaohan. All rights reserved.
//

///网易云设置
#import "AppDelegate+NTESService.h"
#import "NTESBundleSetting.h"
#import "NTESSDKConfigDelegate.h"
#import "LoginViewController.h"
#import "NETSConfig.h"
@implementation AppDelegate (NTESService)

- (void)setupNIMSDK
{
    //配置额外配置信息 （需要在注册 appkey 前完成
    self.sdkConfigDelegate = [[NTESSDKConfigDelegate alloc] init];
    [[NIMSDKConfig sharedConfig] setDelegate:self.sdkConfigDelegate];
    [[NIMSDKConfig sharedConfig] setShouldSyncUnreadCount:YES];
    [[NIMSDKConfig sharedConfig] setMaxAutoLoginRetryTimes:10];
    [[NIMSDKConfig sharedConfig] setMaximumLogDays:[[NTESBundleSetting sharedConfig] maximumLogDays]];
    [[NIMSDKConfig sharedConfig] setShouldCountTeamNotification:[[NTESBundleSetting sharedConfig] countTeamNotification]];
    [[NIMSDKConfig sharedConfig] setAnimatedImageThumbnailEnabled:[[NTESBundleSetting sharedConfig] animatedImageThumbnailEnabled]];
    [[NIMSDKConfig sharedConfig] setFetchAttachmentAutomaticallyAfterReceiving:[[NTESBundleSetting sharedConfig] autoFetchAttachment]];
    [[NIMSDKConfig sharedConfig] setFetchAttachmentAutomaticallyAfterReceivingInChatroom:[[NTESBundleSetting sharedConfig] autoFetchAttachment]];
    
    //多端登录时，告知其他端，这个端的登录类型，目前对于android的TV端，手表端使用。
    [[NIMSDKConfig sharedConfig] setCustomTag:[NSString stringWithFormat:@"%ld",(long)NIMLoginClientTypeiOS]];
    
    //appkey 是应用的标识，不同应用之间的数据（用户、消息、群组等）是完全隔离的。
    NIMSDKOption *option = [NIMSDKOption optionWithAppKey:[[NETSConfig sharedConfig] appKey]];
    option.apnsCername   = [[NETSConfig sharedConfig] apnsCername];
    [[NIMSDK sharedSDK] registerWithOption:option];
    [[NIMSDKConfig sharedConfig] setEnabledHttpsForInfo:NO];
    [[NIMSDKConfig sharedConfig] setEnabledHttpsForMessage:NO];
    [NIMKit sharedKit].config.cellBackgroundColor = [UIColor clearColor];
    [NIMKit sharedKit].config.avatarType = NIMKitAvatarTypeRadiusCorner;
    [NIMKit sharedKit].config.rightBubbleSettings.textSetting.textColor = UIColorHex(333333);
    //注册自定义消息的解析器
    //    [NIMCustomObject registerCustomDecoder:[NTESCustomAttachmentDecoder new]];
    
    //注册 NIMKit 自定义排版配置
//        [[NIMKit sharedKit] registerLayoutConfig:[NTESCellLayoutConfig new]];
    
//    BOOL isUsingDemoAppKey = [[NIMSDK sharedSDK] isUsingDemoAppKey];
//    [[NIMSDKConfig sharedConfig] setTeamReceiptEnabled:isUsingDemoAppKey];
   
    //场景配置
    NSDictionary *dict = @{@"nim_custom1":@1};
    //    NSMutableDictionary *dict1 = [NIMSDK sharedSDK].sceneDict;
    [NIMSDK sharedSDK].sceneDict = (NSMutableDictionary *)dict;
    //    NSMutableDictionary *dict2 = [NIMSDK sharedSDK].sceneDict;
    //    DLog(@"%@,%@",dict1,dict2);
    [[NIMSDK sharedSDK].loginManager addDelegate:self];

}
- (void)autoLogin{
    [[[NIMSDK sharedSDK]loginManager]autoLogin:kCurUser.im_account token:kCurUser.im_token];
}
-(void)onKick:(NIMKickReason)code
   clientType:(NIMLoginClientType)clientType{
    KPostNotification(KNotificationOnKick, @"您的BEES账号在另一个地方登录,您已被迫下线");
}

- (void)onAutoLoginFailed:(NSError *)error
{
    //只有连接发生严重错误才会走这个回调，在这个回调里应该登出，返回界面等待用户手动重新登录。
    DLog(@"onAutoLoginFailed %zd",error.code);
    [self showAutoLoginErrorAlert:error];
}
#pragma mark - 登录错误回调
- (void)showAutoLoginErrorAlert:(NSError *)error
{
    [kUserManager logout:nil];
    [XAlertTool alertTitle:XLocalized(@"BEES自动登录失败") message:[self formatAutoLoginMessage:error] cancelAction:nil sureAction:^{
        [kCurrentVC.navigationController hh_pushBackViewController:[LoginViewController new]];
    }];
}

- (NSString *)formatAutoLoginMessage:(NSError *)error
{
    NSString *message = [NSString stringWithFormat:@"%@",error];
    NSString *domain = error.domain;
    NSInteger code = error.code;
    if ([domain isEqualToString:NIMLocalErrorDomain])
    {
        if (code == NIMLocalErrorCodeAutoLoginRetryLimit)
        {
            message = @"自动登录错误次数超限，请检查网络后重试";
        }
    }
    else if([domain isEqualToString:NIMRemoteErrorDomain])
    {
        if (code == NIMRemoteErrorCodeInvalidPass)
        {
            message = @"密码错误";
        }
        else if(code == NIMRemoteErrorCodeExist)
        {
            message = @"当前已经其他设备登录，请使用手动模式登录";
        }
    }
    return message;
}
@end
